//
//  CZMessageController.m
//  TCProject
//
//  Created by admin on 16/2/2.
//  Copyright © 2016年 wise-wing. All rights reserved.
//

#import "CZMessageController.h"
#import "NoHiligitButton.h"
#import "EmptyView.h"
#import "WebViewController.h"
#import "OrderDetailController.h"

@interface CZMessageController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet NoHiligitButton *msgButton;

@property (weak, nonatomic) IBOutlet NoHiligitButton *noticeButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *indicatorLeftMargin;

- (IBAction)menuClick:(id)sender;

@property (nonatomic, strong) EmptyView *msgEmptyView;
@property (nonatomic, strong) EmptyView *noticeEmptyView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (nonatomic, weak) UIButton *currentButton;
@end

@implementation CZMessageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"消息中心";
    self.scrollView.scrollEnabled = NO;
    
    [self configuration];
    
    [self loadDatas];
}

- (void)loadDatas
{
    
}

- (void)configuration {
    for (int i = 0; i < 2; i++) {
        UITableView *tableView = [[UITableView alloc] init];
        tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [tableView registerNib:[UINib nibWithNibName:@"ZDNoticeCell" bundle:nil] forCellReuseIdentifier:reuseIdentifier];
        tableView.backgroundColor = [UIColor clearColor];
        tableView.tableFooterView = [[UIView alloc] init];
        tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [tableView.header endRefreshing];
            });
        }];
        tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [tableView.footer endRefreshing];
            });
        }];
        tableView.footer.automaticallyHidden = YES;
        tableView.rowHeight = 70.f;
        tableView.tag = i;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.frame = _scrollView.bounds;
        tableView.x = i * self.view.width;
        [_scrollView addSubview:tableView];
    }
    _scrollView.contentSize = CGSizeMake(kScreenWidth * 2, 0);
}

- (EmptyView *)msgEmptyView
{
    if (!_msgEmptyView) {
        _msgEmptyView = [[EmptyView alloc] init];
        _msgEmptyView.message = @"暂无消息哦!";
        _msgEmptyView.image = [UIImage imageNamed:@"empty_msg"];
    }
    return _msgEmptyView;
}

- (EmptyView *)noticeEmptyView
{
    if (!_noticeEmptyView) {
        _noticeEmptyView = [[EmptyView alloc] init];
        _noticeEmptyView.message = @"暂无公告哦!";
        _noticeEmptyView.image = [UIImage imageNamed:@"empty_msg"];
    }
    return _noticeEmptyView;
}

- (IBAction)menuClick:(UIButton *)sender {
    
    if (_currentButton == sender) return;
    
    [_scrollView setContentOffset:CGPointMake(_scrollView.width * sender.tag, 0) animated:YES];
}

#pragma mark - UIScrollViewDelegate代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _scrollView) {
        _indicatorLeftMargin.constant = scrollView.contentOffset.x * 0.5;
        BOOL no = scrollView.contentOffset.x / self.scrollView.width + 0.5 > 1.f;
        _noticeButton.selected = no;
        _msgButton.selected = !no;
        _currentButton = no ? _noticeButton : _msgButton;
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = arc4random_uniform(10);
    
    UIView *targetView = tableView.tag ? self.noticeEmptyView : self.msgEmptyView;
    if (!count) {
        targetView.frame = tableView.bounds;
        [tableView addSubview:targetView];
    } else if (count && targetView.superview) {
        [targetView removeFromSuperview];
    }
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 0) {
        OrderDetailController *detailVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"orderDetailVC"];
        [self showViewController:detailVC sender:nil];
    } else {
        WebViewController *webVC = [WebViewController new];
        webVC.path = [NSURL URLWithString:@"http://www.baidu.com"];
        [self showViewController:webVC sender:nil];
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

@end
